/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package comparator;

import halali.CSFDTitle;
import halali.ShowTitle;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import moviedownloader5.DataSourceProvider;

/**
 *
 * @author Patrik
 */
public class Films {

    private final DataSource ds;
    private final Connection conn;
    private ResultSet filmy_result;
    
    private final HashMap<Integer, List<String>> films;
    
    private final List<CSFDTitle> titles = new ArrayList<>(400000);

    public Films() {
        this.ds = DataSourceProvider.getDataSource();
        try {
            conn = ds.getConnection();
            conn.setAutoCommit(true);
        } catch (SQLException ex) {
            throw new IllegalArgumentException(ex);
        }
        
        this.films = new HashMap<>();
        
        try {
            Statement filmy_statement = conn.createStatement();
            this.filmy_result = filmy_statement.executeQuery("select * from filmy f, nazvy_filmov nf where f.film_id = nf.film_id");
            
            while(filmy_result.next()){
                
                int film_id = filmy_result.getInt("film_id");
                String film_name = filmy_result.getString("nazov");
                
                CSFDTitle st = new CSFDTitle(film_name, film_id);
                titles.add(st);
                
                //MATO ZAKOMENTOVAL
               /*  if(films.containsKey(film_id)){
                    films.get(film_id).add(film_name);
                }else{
                    List<String> names = new ArrayList<>();
                    names.add(film_name);
                    films.put(film_id, names);
                }*/
            }
            
            /*
            for (int key : films.keySet()) {
                for (String str : films.get(key)) {
                    System.out.println(key + " " + str.split("[(]")[0]);
                }
            }
            */
            
        } catch (SQLException ex) {
            Logger.getLogger(Matcher.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public HashMap<Integer, List<String>> getFilms() {
        synchronized(this.films){
            return this.films;
        }
    }
    
    public List<String> getNames(int id){
        synchronized(this.films){
            return this.films.get(id);
        }
    }

    public List<CSFDTitle> getTitles() {
        return titles;
    }
    
    
    
    
    
}
